# -*- coding:utf-8 -*-
import json
import requests
import pymysql
import os
from urllib.parse import urljoin
import time
import urllib3

urllib3.disable_warnings()  # 禁用SSL警告

#需求: 爬取https://tuchong.com/category/%E4%BA%BA%E5%83%8F网址的人像图片下载到mysql
class Spider:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
            'Referer': 'https://tuchong.com/',
            'Cookie': 'ttcid=9362ddf373364337b1f33cd0bb70513c22; log_web_id=6293662498; token=0231693db9fa45e3; _ga=GA1.2.1917165682.1730685224; __utma=115147160.1917165682.1730685224.1730685224.1730685224.1; __utmz=115147160.1730685224.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); s_v_web_id=verify_m32dapuy_4UvNfx74_NNRc_4YeD_835e_1TZl73u6hpZs; _ga_FJEZRW3PBC=GS1.2.1730685225.1.0.1730685225.0.0.0; webp_enabled=1; lang=zh; PHPSESSID=b33m46795b4rvrcq5l77ov54mc; tt_scid=o-2PGRvTi51NRqTsrwK9bFyTIMXtJ55N1z2YVYE6QnW8-lS6QyfYR1-0pfhHbZHA214f'  # 重要：需要添加你的登录Cookie
        }

        # 数据库配置
        self.db_config = {
            "host": "127.0.0.1",
            "user": "root",
            "password": "root",
            "database": "cars",
            "charset": "utf8mb4"
        }

    def get_data(self, url):
        try:
            response = requests.get(url, headers=self.headers, timeout=10)
            if response.status_code != 200:
                print(f"请求失败，状态码: {response.status_code}")
                return

            data = response.json()
            if data.get('result') == 'ERROR':
                print(f"获取数据失败: {data.get('message')}")
                return

            feed_list = data.get('feedList', [])
            for feed in feed_list:
                try:
                    image_url = feed.get('url', '')
                    if image_url:
                        self.save_to_db(image_url)
                        print(f"保存URL成功: {image_url}")
                except Exception as e:
                    print(f"处理数据项时出错: {str(e)}")
                    continue

        except requests.RequestException as e:
            print(f"请求出错: {str(e)}")
        except json.JSONDecodeError as e:
            print(f"JSON解析错误: {str(e)}")
        except Exception as e:
            print(f"其他错误: {str(e)}")

    def save_to_db(self, url):
        try:
            with pymysql.connect(**self.db_config) as conn:
                with conn.cursor() as cursor:
                    # 确保表存在
                    cursor.execute("""
                        CREATE TABLE IF NOT EXISTS tuchong_images (
                            id INT AUTO_INCREMENT PRIMARY KEY,
                            image_url VARCHAR(255) NOT NULL,
                            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                        )
                    """)

                    # 插入数据
                    sql = "INSERT INTO tuchong_images (image_url) VALUES (%s)"
                    cursor.execute(sql, (url,))
                    conn.commit()

        except pymysql.Error as e:
            print(f"数据库错误: {str(e)}")


if __name__ == '__main__':
    spider = Spider()
    url = "https://tuchong.com/rest/categories/%E4%BA%BA%E5%83%8F/recommend"
    spider.get_data(url)
